package com.venus.finance.account.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.venus.common.action.BaseAction;
import com.venus.common.utils.DateUtil;
import com.venus.common.utils.JsonUtil;
import com.venus.finance.account.form.AccountForm;
import com.venus.finance.account.service.IAccountService;
import com.venus.model.AccountVO;

public class AccountMultiOperateAction extends BaseAction {

	private IAccountService accountService;

	public void setAccountService(IAccountService accountService) {
		this.accountService = accountService;
	}
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		AccountForm objAccountForm = (AccountForm)form;
		String strMethod = objAccountForm.getMethod();
		log.info("调用方法:"+strMethod);
		if("list".equals(strMethod)){
			return this.list(mapping, objAccountForm, request, response);
		}else if("create".equals(strMethod)){
			return this.create(mapping, objAccountForm, request, response);
		}else if("insert".equals(strMethod)){
			return this.insert(mapping, objAccountForm, request, response);
		}else if("edit".equals(strMethod)){
			return this.edit(mapping, objAccountForm, request, response);
		}else if("update".equals(strMethod)){
			return this.update(mapping, objAccountForm, request, response);
		}else if("delete".equals(strMethod)){
			return this.delete(mapping, objAccountForm, request, response);
		}else if("getAccount4Ajax".equals(strMethod)){
			return this.getAccount4Ajax(mapping, objAccountForm, request, response);
		}
		return super.execute(mapping, form, request, response);
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.saveToken(request);
		//AccountForm objAccountForm = (AccountForm)form;
		List<AccountVO> lstAccountVOs = this.accountService.findAccountByUser(this.getLoginUser(request));
		request.setAttribute("lstAccountVOs", lstAccountVOs);
		return mapping.findForward("list");
	}
	
	public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.saveToken(request);
		//AccountForm objAccountForm = (AccountForm)form;
		return mapping.findForward("create");
	}
	
	public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		AccountForm objAccountForm = (AccountForm)form;
		AccountVO objAccountVO = new AccountVO();
		objAccountVO.setAccountName(objAccountForm.getAccountName());
		objAccountVO.setAmount(objAccountForm.getAmount());
		objAccountVO.setArea(objAccountForm.getArea());
		objAccountVO.setCreateDate(DateUtil.parseDate(objAccountForm.getCreateDate(), "yyyy-MM-dd"));
		objAccountVO.setNote(objAccountForm.getNote());
		objAccountVO.setPubUser(this.getLoginUser(request));
		objAccountVO.setSort(0);
		this.accountService.saveAccountVO(objAccountVO);
		request.setAttribute("newAccountId", objAccountVO.getAccountId());
		return this.list(mapping, objAccountForm, request, response);
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.saveToken(request);
		AccountForm objAccountForm = (AccountForm)form;
		AccountVO objAccountVO = this.accountService.loadAccountVO(objAccountForm.getAccountId());
		request.setAttribute("objAccountVO", objAccountVO);
		return mapping.findForward("edit");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		AccountForm objAccountForm = (AccountForm)form;
		AccountVO objAccountVO = this.accountService.loadAccountVO(objAccountForm.getAccountId());
		objAccountVO.setAccountName(objAccountForm.getAccountName());
		objAccountVO.setAmount(objAccountForm.getAmount());
		objAccountVO.setArea(objAccountForm.getArea());
		objAccountVO.setCreateDate(DateUtil.parseDate(objAccountForm.getCreateDate(), "yyyy-MM-dd"));
		objAccountVO.setNote(objAccountForm.getNote());
		objAccountVO.setSort(0);
		this.accountService.saveOrUpdateAccountVO(objAccountVO);
		request.setAttribute("updateAccountId", objAccountVO.getAccountId());
		return this.list(mapping, objAccountForm, request, response);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		AccountForm objAccountForm = (AccountForm)form;
		this.accountService.deleteAccountVO(objAccountForm.getAccountIds());
		return this.list(mapping, form, request, response);
	}
	
	public ActionForward getAccount4Ajax(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		//AccountForm accountForm = (AccountForm)form;
		// 得到拥有的所有账户信息
		List<AccountVO> lstAccountVOs = this.accountService.findAccountByUser(this.getLoginUser(request));
		String strJson = JsonUtil.getJsonString4List(lstAccountVOs, new String[]{"pubUser"});
		// 将json返回给页面
		return this.forwardAjax(strJson, response);
	}
}
